package com.yztstudy.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yztstudy.Entity.AdminUser;
import com.yztstudy.Mapper.AdminUserMapper;
import com.yztstudy.Param.AdminUserParam;
import com.yztstudy.Service.AdminUserService;
import com.yztstudy.clients.UserFeignClient;
import com.yztstudy.constants.UserConstants;
import com.yztstudy.entity.User;
import com.yztstudy.param.CartListParam;
import com.yztstudy.param.PageParam;
import com.yztstudy.utils.MD5Util;
import com.yztstudy.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class AdminUserServiceImpl implements AdminUserService {

    @Autowired
    private AdminUserMapper adminUserMapper;

    @Resource
    private UserFeignClient userFeignClient;

    /**
     * TODO 校验用户名和密码
     * @param adminUserParam 用户名和密码，还有验证码
     * @return
     */
    @Override
    public AdminUser login(AdminUserParam adminUserParam) {

        // TODO 1.构造查询条件
        QueryWrapper<AdminUser> wrapper = new QueryWrapper<>();
        wrapper.eq("user_account", adminUserParam.getUserAccount());

        // TODO 2.密码进行加盐处理
        wrapper.eq("user_password", MD5Util.encode(adminUserParam.getUserPassword() + UserConstants.USER_SLAT));

        // TODO 3.查询
        AdminUser user = adminUserMapper.selectOne(wrapper);
        log.info("AdminUserServiceImpl.login方法执行完毕,结果为:{}", user);
        return user;
    }


    /**
     * TODO 分页查询用户
     * @param pageParam 分页参数
     * @return
     */
    @Override
    @Cacheable(value = "list.user", key = "#pageParam.currentPage + '-' + #pageParam.pageSize", cacheManager = "cacheManagerHour")
    public R userList(PageParam pageParam) {

        // TODO 1.远程调用用户服务，分页查询部分用户
        R result = userFeignClient.listPage(pageParam);

        // TODO 2.日志记录
        if (result.getCode().equals(R.SUCCESS_CODE))
        {
            log.info("AdminUserServiceImpl.login方法执行完毕,结果为:{}", "用户分页查询成功!");
        }else {
            log.info("AdminUserServiceImpl.login方法执行完毕,结果为:{}", "用户分页查询失败!");
        }
        return result;
    }


    /**
     * TODO 删除用户
     * @param cartListParam 用户主键
     * @return
     */
    @CacheEvict(value = "list.user", allEntries = true) // 清空缓存
    @Override
    public R remove(CartListParam cartListParam) {

        // TODO 1.远程调用用户服务，删除用户
        R result = userFeignClient.remove(cartListParam);

        // TODO 2.日志记录
        if (result.getCode().equals(R.SUCCESS_CODE))
        {
            log.info("AdminUserServiceImpl.remove方法执行完毕,结果为:{}", "用户删除成功!");
        }else {
            log.info("AdminUserServiceImpl.remove方法执行完毕,结果为:{}", "用户删除失败!");
        }
        return result;
    }


    /**
     * TODO 更新用户
     * @param user 用户
     * @return
     */
    @CacheEvict(value = "list.user", allEntries = true) // 清空缓存
    @Override
    public R update(User user) {
        // TODO 1.远程调用用户服务，更新用户
        R result = userFeignClient.update(user);

        // TODO 2.日志记录
        if (result.getCode().equals(R.SUCCESS_CODE))
        {
            log.info("AdminUserServiceImpl.update方法执行完毕,结果为:{}", "用户更新成功!");
        }else {
            log.info("AdminUserServiceImpl.update方法执行完毕,结果为:{}", "用户更新失败!");
        }
        return result;
    }


    /**
     * TODO 添加用户
     * @param user 用户
     * @return
     */
    @CacheEvict(value = "list.user", allEntries = true) // 清空缓存
    @Override
    public R save(User user) {

        // TODO 1.远程调用用户服务，新增用户
        R result = userFeignClient.save(user);

        // TODO 2.日志记录
        if (result.getCode().equals(R.SUCCESS_CODE))
        {
            log.info("AdminUserServiceImpl.save方法执行完毕,结果为:{}", "用户新增成功!");
        }else {
            log.info("AdminUserServiceImpl.save方法执行完毕,结果为:{}", "用户新增失败!");
        }
        return result;
    }
}
